﻿using System;
using System.Threading;
using Microsoft.Phone.Controls;
using XBMCRemote.DataModel;

namespace XBMCRemote
{
    public partial class MainPage : PhoneApplicationPage
    {
        private XbmcHelper _xbmcHelper;

        public MainPage()
        {
            InitializeComponent();
            _xbmcHelper = (App.Current as App).XbmcHelper;
            _xbmcHelper.SetConnected += XbmcHelperSetConnected;

            _xbmcHelper.Connect();
        }

        private void XbmcHelperSetConnected(object sender, EventArgs e)
        {
            string message = "";
            if (_xbmcHelper.Connected)
            {
                message = "Connected to XBMC";
                Dispatcher.BeginInvoke(() => { StatusTextBlock.Text = message; });
                Dispatcher.BeginInvoke(() => NavigationService.Navigate(new Uri("/HomePage.xaml", UriKind.Relative)));
            }
            else
            {
                message = "Could not connect to XBMC. Please check you settings.";
                Dispatcher.BeginInvoke(() => { StatusTextBlock.Text = message; });
            }
        }

        private void btnConnect_Click(object sender, System.EventArgs e)
        {
            _xbmcHelper.Connect();
        }

        private void btnSettings_Click(object sender, System.EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Pages/ConnectionsPage.xaml", UriKind.Relative));
        }

        private void btnAbout_Click(object sender, System.EventArgs e)
        {
            // TODO: Add event handler implementation here.
        }
    }
}
